package fr.unice.poo.modele;

import java.sql.*;

import java.util.*;

public class ModelCookies extends AbstractModelCookies {
	private List<FirefoxCookies> firefoxCookies;

	private static Connection c = new ConnectionFactory().getConnection();

	@Override
	public void delete(String name) {
		Statement st;
		try {
			st=c.createStatement() ;
			  st.executeUpdate ("delete from moz_cookies where name= '"+ name + "'") ;
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
			 

	}

	@Override
	public void deleteAll() {
		Statement st;
		try {
			st=c.createStatement() ;
			  st.executeUpdate ("delete from moz_cookies ") ;
		}
		catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void afficherValeursCookie(String name) {
		Statement st;
		try {
			st=c.createStatement() ;
			 ResultSet rs = st.executeQuery ("select id from moz_cookies where name= '"+ name + "'") ;
			 FirefoxCookies firefoxCookie;
			 
			 while (rs.next()){
				 
				 // Remplir la liste des firefoxCookies
				 
				 firefoxCookie=new FirefoxCookies();
				 firefoxCookie.setId(rs.getInt("id"));
				 firefoxCookie.setValue(rs.getString("Value"));
				 firefoxCookie.setName(rs.getString("Name"));
				 firefoxCookie.setBaseDomain(rs.getString("baseDomain"));
				 firefoxCookie.setHost(rs.getString("host"));
				 firefoxCookie.setExpiry(rs.getDouble("expiry"));
				 firefoxCookie.setPath(rs.getString("path"));
				
				  
			 } 
			 }
			 catch (SQLException e) {
					e.printStackTrace();
				}

			 
		
	}

	public java.util.Date convertTime(long time) {
		long timeStamp = time;
		java.util.Date t = new java.util.Date((long) timeStamp * 1000);
		return t;
	}

	@Override
	public void afficherCookies() {

		firefoxCookies = new ArrayList<FirefoxCookies>();
		Statement st;
		try {
			st = c.createStatement();
			ResultSet rs = st.executeQuery("select id from moz_cookies");
			FirefoxCookies firefoxCookie;
			while (rs.next()) {

				// Remplir la liste des firefoxCookies

				firefoxCookie = new FirefoxCookies();
				firefoxCookie.setId(rs.getInt("id"));
				firefoxCookie.setValue(rs.getString("Value"));
				firefoxCookie.setName(rs.getString("Name"));
				firefoxCookie.setBaseDomain(rs.getString("baseDomain"));
				firefoxCookie.setHost(rs.getString("host"));
				firefoxCookie.setExpiry(rs.getDouble("expiry"));
				firefoxCookie.setPath(rs.getString("path"));

				firefoxCookies.add(firefoxCookie);
			}
			notifyObserver();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void afficherCookiesParSite(String s) {
		Statement st;
		try {
			st=c.createStatement() ;
			 ResultSet rs = st.executeQuery ("select id from moz_cookies where baseDomain= '"+ s + "'") ;
			 FirefoxCookies firefoxCookie;
			 
			 while (rs.next()){
				 
				 // Remplir la liste des firefoxCookies
				 
				 firefoxCookie=new FirefoxCookies();
				 firefoxCookie.setId(rs.getInt("id"));
				 firefoxCookie.setValue(rs.getString("Value"));
				 firefoxCookie.setName(rs.getString("Name"));
				 firefoxCookie.setBaseDomain(rs.getString("baseDomain"));
				 firefoxCookie.setHost(rs.getString("host"));
				 firefoxCookie.setExpiry(rs.getDouble("expiry"));
				 firefoxCookie.setPath(rs.getString("path"));
				
				  
			 } 
			 }
			 catch (SQLException e) {
					e.printStackTrace();
				}


	}

	@Override
	public void afficherExpiry(double s) {
		Statement st;
		try {
			st=c.createStatement() ;
			 ResultSet rs = st.executeQuery ("select id from moz_cookies where expiry >'"+ s + "'") ;
			 FirefoxCookies firefoxCookie;
			 
			 while (rs.next()){
				 
				 // Remplir la liste des firefoxCookies
				 
				 firefoxCookie=new FirefoxCookies();
				 firefoxCookie.setId(rs.getInt("id"));
				 firefoxCookie.setValue(rs.getString("Value"));
				 firefoxCookie.setName(rs.getString("Name"));
				 firefoxCookie.setBaseDomain(rs.getString("baseDomain"));
				 firefoxCookie.setHost(rs.getString("host"));
				 firefoxCookie.setExpiry(rs.getDouble("expiry"));
				 firefoxCookie.setPath(rs.getString("path"));
				
				  
			 } 
			 }
			 catch (SQLException e) {
					e.printStackTrace();
				}


	}

}
